<?php
	define('CONFIGURED', true);

	class Config {
		public $Root;
		
		function __construct() {
			$this -> Root = $this -> parseFile(); 
		}
		
		private function createConstants($config) {
			// For convenience only
			define('FTSE_FEED', $config['Feeds']['Ftse'][0]);
			define('NEWS_FEED', $config['Feeds']['News'][0]);
			
			define('DB_HOST', $config['Database']['Host']);
			define('DB_USER', $config['Database']['User']);
			define('DB_PASS', $config['Database']['Pass']);
			define('DB_NAME', $config['Database']['Name']);
			
			define('SERVER_HOST', $config['Server']['Host']);
			define('SERVER_PORT', $config['Server']['Port']);
			define('SERVER_MSG_SIZE', $config['Server']['MaxBytes']);
		}
		
		private function parseFile() {
			$path = realpath(((basename(getcwd()) == 'server') ? '' : '../') . 'config/root.conf');
			$contents = file($path);
			$conf = array();
			
			foreach ($contents as $line) {
				$line = trim($line);
				if (strlen($line) < 1 || strpos($line, '#') === 0) continue;
				
				if (strpos($line, '[') !== false) {
					$head = str_replace(array('[', ']'), '', $line);
					$config[$head] = array();
				} else {
					$split = explode(' =', $line);
					
					if (($i = strrpos($split[1], ':')) !== false) {
						$config[$head][$split[0]] = explode(':', $split[1]);
						$config[$head][$split[0]][0] = trim($config[$head][$split[0]][0]);
						$config[$head][$split[0]][1] = (int) $config[$head][$split[0]][1];
					} else {
						$config[$head][$split[0]] = trim($split[1]);
					}
				}
			}
			
			$this -> createConstants($config);
			
			return $config;
		}
	}
?>